<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6" :offset="6">
          <el-input
            placeholder="请输入内容"
            clearable
            @clear="querBtn"
            v-model="userList.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="querBtn"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="6">
          <el-button type="primary" @click="ClosePage = true"
            >主要按钮</el-button
          >
        </el-col>
      </el-row>

      <el-table :data="userItem" border stripe>
        <el-table-column type="index" label="id"> </el-table-column>
        <el-table-column label="姓名" prop="username"> </el-table-column>
        <el-table-column label="邮箱" prop="email"> </el-table-column>
        <el-table-column label="电话" prop="mobile"> </el-table-column>
        <el-table-column label="角色" prop="role_name"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="observe(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              :enterable="false"
              content="人员管理"
              placement="top"
            >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-share"
              ></el-button>
            </el-tooltip>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userList.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="userList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="添加用户" :visible.sync="ClosePage">
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账户名称" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="账户密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="账户邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="ClosePage = false">取 消</el-button>
        <el-button type="primary" @click="ClosePage = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // var checkEail = (rule, value, cd) => {
    //   const addEail =
    //     /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$ /;
    //   if (addEail.test(value)) return cd();
    //   cd(new Error("邮箱格式错误,请输入正确的邮箱格式"));
    // };
   var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
    return {
      ClosePage: false,
      userList: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      userItem: [],
      total: 0,
      addForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      addRules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async querBtn() {
      const { data: res } = await this.$http.get("users", {
        params: this.userList,
      });
      console.log(res.data.total);
      this.userItem = res.data.users;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      this.userList.pagesize = val;
      this.querBtn();
    },
    handleCurrentChange(val) {
      console.log(222, val);
      this.userList.pagenum = val;
      this.querBtn();
    },
    async observe(val) {
      const { data: res } = await this.$http.put(
        `users/${val.id}/state/${val.mg_state}`
      );
      console.log(res);

      if (res.meta.status !== 200) {
        val.mg_state = !val.mg_state;
        return this.$Message.error("状态更新失败");
      }
      this.$Message.success("状态更新成功");
    },
  },
  computed: {
    user() {},
  },
  mounted() {
    this.querBtn();
  },
};
</script>

<style scoped></style>
<style lang="scss" scoped>
.el-table {
  margin-top: 10px;
}
.el-pagination {
  margin: 10px auto;
}
</style>
